package edu.usx.server.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import edu.usx.server.mapper.MsgMapper;
import edu.usx.server.pojo.Msg;
import edu.usx.server.pojo.MsgContent;
import edu.usx.server.pojo.PageBean;
import edu.usx.server.service.IMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author  聂枫
 * @since 2024-07-29
 */



@Service
public class MsgServiceImpl implements IMsgService {

    @Autowired
    private MsgMapper msgMapper;

    @Override
    public PageBean<MsgContent> findMsgByUserId(Integer pageNum, Integer pageSize, Integer userId) {
        PageBean<MsgContent> pageBean = new PageBean<>();

        PageHelper.startPage(pageNum, pageSize);

        List<MsgContent> msgList =msgMapper.findMsgByUserId(userId);

        //count统计未读msg总数
        Integer count= msgMapper.findUnReadMsgCountByUserId(userId);

        for(MsgContent msgContent : msgList){
            msgContent.setState(msgMapper.findMsgStateByUserIdAndMsgId(userId,msgContent.getId()).getState());
        }


        Page<MsgContent> page=(Page<MsgContent>) msgList;

        pageBean.setPageTotal(page.getPages());
        pageBean.setCurrentPage(page.getPageNum());
        pageBean.setMsgList(page.getResult());
        pageBean.setUnReadMsgNum(count);

        return pageBean;
    }

    @Override//一键已读
    public void updateAllMsgReadByUserId(Integer userId) {
       msgMapper.updateAllMsgState(userId);
    }
}
